<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item prop="nickName" label="姓名">
      <el-input v-model="form.nickName" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item prop="phoneNum" label="手机号码">
      <el-input v-model="form.phoneNum" placeholder="请输入手机号码" />
    </el-form-item>
    <el-form-item prop="email" label="电子邮箱">
      <el-input v-model="form.email" placeholder="请输入电子邮箱" />
    </el-form-item>
    <el-form-item prop="sex" label="性别">
      <el-select v-model="form.sex" class="w_100" placeholder="请选择性别">
        <el-option :value="1" label="男" />
        <el-option :value="2" label="女" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import { mapGetters } from 'vuex'
import _ from 'lodash'
import { edit } from '@/api/user'

export default {
  name: 'BasicInfo',
  data() {
    return {
      form: {
        name: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户账号', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        nickName: [{ required: true, message: '请输入用户姓名', trigger: 'blur' }],
        phoneNum: [{ validator: this.checkPhone, required: false, trigger: 'blur' }],
        email: [{ validator: this.checkEmail, required: false, trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  created() {
    this.form = _.cloneDeep(this.userInfo)
  },
  methods: {
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          edit(this.form).then(res => {
            this.$message.success('保存成功')
            this.$store.dispatch('user/getInfo')
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
